package yy.com.yjboview.viewutil;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import yy.com.yjboview.R;

/**
 * @description: <描述当前版本功能>
 * <p>
 * 简单的继承view类--基础知识
 * 引用：http://www.idtkm.com/customview/customview1/
 * </p>
 * @author: yjbo
 * @date: 2016-08-03 11:09
 */
public class SimpleViewUtil extends View {

    Canvas mcanvas;

    public SimpleViewUtil(Context context) {
        super(context);
    }

    public SimpleViewUtil(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SimpleViewUtil(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)//21版本
    public SimpleViewUtil(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    //绘制过程
    //布局1
//    UNSPECIFIED	0	父容器不对 view 有任何限制，要多大给多大
//    EXACTLY	1	父容器已经检测出 view 所需要的大小,比如固定大小xxdp
//    AT_MOST	2	父容器指定了一个大小， view 的大小不能大于这个值
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //Width
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);//宽度值
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);//宽度测量模式
        //Height
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);//高度值
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);//高度测量模式
        Log.d("yjbo-onMeasure", "widthMode==" + widthMode);
        Log.d("yjbo-onMeasure", "widthSize==" + widthSize);
        Log.d("yjbo-onMeasure", "heightMode==" + heightMode);
        Log.d("yjbo-onMeasure", "heightSize==" + heightSize);
    }

    //布局1
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.d("yjbo-onLayout", "getLeft==" + getLeft());
        Log.d("yjbo-onLayout", "getTop==" + getTop());
        Log.d("yjbo-onLayout", "getRight==" + getRight());
        Log.d("yjbo-onLayout", "getBottom==" + getBottom());
    }

    //布局1
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mcanvas = canvas;
        Log.d("yjbo-onDraw", "onDraw==");

        //绘制图形
        Paint paint = new Paint();
        paint.setColor(getResources().getColor(R.color.colorAccent));
        canvas.drawCircle(200, 500, 100, paint);
        paint.setTextSize(50);
        paint.setColor(getResources().getColor(R.color.colorPrimary));
        canvas.drawText("绘制图形", 200, 500, paint);

        //绘制文本
        Paint paint1 = new Paint();
        paint1.setTextSize(50);
        paint1.setColor(getResources().getColor(R.color.colorPrimary));
        canvas.drawText("绘制文本", 500, 200, paint1);

        //画布变换
        Paint paint2 = new Paint();
        paint2.setTextSize(30);
        paint2.setAntiAlias(true);
        paint2.setColor(getResources().getColor(R.color.colorPrimary));
        canvas.drawText("画布变换-平移-画布变换****坑锯齿)", 10, 700, paint2);

        Paint paint3 = new Paint();
        paint3.setTextSize(30);
        paint3.setAntiAlias(false);
        paint3.setColor(getResources().getColor(R.color.colorPrimary));
        canvas.drawText("画布变换-平移-画布变换****不坑锯齿", 10, 800, paint3);
        canvas.translate(20, 30);

        //画布裁剪
        canvas.clipRect(300, 400, 700, 500);
    }

    //事件处理
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);

    }

    //重绘
    @Override
    public void invalidate() {
        super.invalidate();

    }

    public void changeDraw(int i) {
        switch (i) {
            case 0:

                break;
        }
    }
}
